﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.Weather.Interfaces;

namespace DispatchAssist.Weather.Implements
{
    class NmcCountrywideRadarWeatherMapInfo : IGetPics
    {
        public DateTime Date { get; private set; }

        public IWeatherMapInfo WeatherMapInfo { get; private set; }
        public const int DisplayInterval = 30;

        public const int MapInterval = 10;

        public NmcCountrywideRadarWeatherMapInfo(IWeatherMapInfo mapInfo)
        {
            WeatherMapInfo = mapInfo;

            Date = DateTime.Now.AddMinutes(-DisplayInterval);
        }

        public void SetPreviousDateTime()
        {
            Date = Date.AddMinutes(-DisplayInterval);
        }

        public string GetNewestPic()
        {
            var str = GetPic(Date);
            CarolLib.Log.Instance.Info("区域最新图片地址：" + str);
            return str;
        }

        private string GetPic(DateTime date)
        {
            //全国：
            //http://image.weather.gov.cn/product/2011/201109/20110905/RDCP/medium/SEVP_NMC_RDCP_SLDAS_EZ9_ACHN_L88_PI_20110905021000000.JPG
            //http://image.weather.gov.cn/product/2011/201109/20110905/RDCP/medium/SEVP_NMC_RDCP_SLDAS_EZ9_ACHN_L88_PI_20110905000000000.JPG


            //东北
            //http://image.weather.gov.cn/product/2011/201109/20110905/RDCP/medium/SEVP_NMC_RDCP_SLDAS_EZ9_ANEC_L88_PI_20110905021000000.JPG

            //上海
            //http://image.weather.gov.cn/product/2011/201109/20110905/RDCP/medium/SEVP_AOC_RDCP_SLDAS_EBREF_AZ9210_L88_PI_20110905023500000.GIF
            //北京
            //http://image.weather.gov.cn/product/2011/201109/20110905/RDCP/medium/SEVP_AOC_RDCP_SLDAS_EBREF_AZ9010_L88_PI_20110905022500000.GIF

            return string.Format(@"http://image.weather.gov.cn/product/{1}/RDCP/medium/SEVP_NMC_RDCP_SLDAS_EZ9_{0}_L88_PI_{2}{3}.JPG",
               WeatherMapInfo.InfoID,  GetDate1(date), GetDate2(date), GetDate3(date));
        }
        private string GetDate1(DateTime date)
        {
            if (date.Hour < 8)
            {
                date = date.AddDays(-1);
            }
            return date.ToString("yyyy/yyyyMM/yyyyMMdd");
        }
        private string GetDate2(DateTime date)
        {
            if (date.Hour < 8)
            {
                date = date.AddDays(-1);
            }
            return date.ToString("yyyyMMdd");
        }
        private string GetDate3(DateTime date)
        {
            //从8点开始，显示半个小时前的图片
            //前一天的160000-今天的155000为当天时间
            //000000	08:00:00
            //001000	08:10:00
            //002000	08:20:00
            //003000	08:30:00
            //010000	09:00:00
            //020000	10:00:00
            //030000	11:00:00
            //050000	13:00:00
            //20110905021000000
            return string.Format("{0:00}{1}000000", date.Hour >= 8 ? (date.Hour - 8) : (date.Hour + 16), date.Minute / MapInterval);

        }

        public IList<string> GetAnimatePics(double time)
        {
            IList<string> pics = new List<string>();

            var beginDate = Date.AddHours(-time);

            CarolLib.Log.Instance.Info(string.Format("区域天气动画图片地址开始,图片开始时间{0}", beginDate.ToString("yyyyMMdd HH:mm:ss")));
            for (DateTime startDate = beginDate; startDate <= Date; startDate = startDate.AddMinutes(MapInterval))
            {
                var str = GetPic(startDate);
                CarolLib.Log.Instance.Info("区域天气动画图片地址：" + str);
                pics.Add(str);
            }
            return pics;
        }
    }
}
